import os
import sys
import cProfile
from copy import copy

from common import *
from AI import *
from Environment import *

e = Environment()
p1 = AI("1")
p2 = AI("2")

#p1.patterns.append(CreatePatternFromIds([0, 2, 3, 4]))
#p2.patterns.append(CreatePatternFromIds([1, 3, 2, 4]))

#p1.patterns.append(CreatePatternFromIds([0, 2, 3, 4]))
#p2.step(States[0])
#print "1: ", p1.history, p1.patterns
#print "2: ", p2.history, p2.patterns
#p2.ask(p1)
#print "1: ", p1.history, p1.patterns
#print "2: ", p2.history, p2.patterns
#sys.exit()

states = e.statesInCycle()

#print p1.predict(states[0:7])
#sys.exit()

totalFitness1 = 0
totalFitness2 = 0
for n in range(400):
    p1.reset()
    p2.reset()
    accFitness1 = 0
    accFitness2 = 0
    for i in range(len(states)):
        p1.step(states[i])
        ip2 = (i + 4) % 8
        p2.step(states[ip2])
        if p1.action == Action.Ask:
            p1.ask(p2)
        if p2.action == Action.Ask:
            p2.ask(p1)
        fitness1 = p1.fitness(states)
        fitness2 = p2.fitness(states)
        p1.learnFromFitnessAfterAction(fitness1)
        p2.learnFromFitnessAfterAction(fitness2)
        accFitness1 = accFitness1 + fitness1
        accFitness2 = accFitness2 + fitness2
        #print p1.fitness(states), p2.fitness(states)
        #print p1.fitness(states)
    totalFitness1 = totalFitness1 + accFitness1
    totalFitness2 = totalFitness2 + accFitness2
    #print n, accFitness1, accFitness2
    print n, p1.stateActionValues[(States[0].id, Action.NoAction)], \
          p1.stateActionValues[(States[0].id, Action.Dream)], \
          p1.stateActionValues[(States[0].id, Action.Ask)], \
          p1.stateActionValues[(States[1].id, Action.NoAction)], \
          p1.stateActionValues[(States[1].id, Action.Dream)], \
          p1.stateActionValues[(States[1].id, Action.Ask)]

#print p1.stateActionValues
#print totalFitness1
#print totalFitness2
#print p1.patterns:
#print p2.patterns:

